package com.yuexunit.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.timevale.esign.sdk.tech.bean.result.Result;
import com.timevale.esign.sdk.tech.service.EsignsdkService;
import com.timevale.esign.sdk.tech.service.factory.EsignsdkServiceFactory;
import com.timevale.tech.sdk.bean.ProjectConfig;
import com.yuexunit.core.ConfigPropertites;
import com.yuexunit.finance.member.service.EnterpriseService;
import com.yuexunit.finance.project.action.v1.FinanceSignatureActionApi;
import com.yuexunit.finance.project.service.FinanceSignatureService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.persist.annotation.Transactional;



/** 
 * @ClassName: StartServlet  初始化并登入接口
 * @author: NTBK-021-1010
 * @date: 2016年12月6日 上午9:37:31  
 */
public class InitServlet extends HttpServlet {

	  private static final long serialVersionUID = -8422925953756936539L;
	  
	  private Logger LOG = LoggerFactory.getLogger(InitServlet.class);
	  
	  private EsignsdkService SDK = EsignsdkServiceFactory.instance();
	  
//	  private AccountService SERVICE = AccountServiceFactory.instance();
	  
	  EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
	  
	  FinanceSignatureService signatureService= ComponentFactory.loadClass(FinanceSignatureService.class);
	  
	  FinanceSignatureActionApi financeSignatureActionApi= ComponentFactory.loadClass(FinanceSignatureActionApi.class);
	
	  
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

//        req.getSession().setAttribute("eaccountId", loginResult.getAccountId());
	}
	  
	@Override
	@Transactional
	public void init() throws ServletException {
		// 功能暂时不上线使用
		LOG.info("开始执行初始化...");
        String projectId = ConfigPropertites.getProperties("PROJECTID");
        String projectSecret =ConfigPropertites.getProperties("PROJECT_SECRET");
        LOG.info("projectId:"+projectId);
		LOG.info("projectSecret"+projectSecret);
//        Result result = SDK.init(projectId, projectSecret);
        ProjectConfig pc = new ProjectConfig();
        pc.setProjectId(projectId);
        pc.setProjectSecret(projectSecret);
        pc.setItsmApiUrl("http://itsm.tsign.cn/tgmonitor/rest/app!getAPIInfo2");
        
//        SignatureConfig sc = new SignatureConfig();
//        
//        sc.setAlgorithm(AlgorithmType.RSA);
//        sc.setEsignPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6HN9Sj+Js4W4Eg7G+CZ15w8TmIMkeVaNrTkbPvPbPQez5hl+p8MsQa3sCaaGmBocygbMvUmazTbAmuq2RNyUh7L29u9JrkVQeiWBwR/Z8PM0005lvCPS9d1dkRgNuXevMQeS0bcLopz7C5hMD1x1qWKpAc2gAVcd/VgqUeuSTHDZnm/YV00MXcxXf/mMaJkerIhorlHE8spyuLW5RgVvrq2pr1N4R75wCzI8KG9xTtPSPyNAwvP+cVrA8CjeuR/N1CdHcoggsAKqh2Cvo/toHBLr0rvlvVTH0nA0yFJnwh+1po4ft40l9tHPCMZTc02BbXqzBgdxSWbGrEgpe5Id4wIDAQAB");
//        sc.setPrivateKey("MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA4yFBEiTu31l9Ds9tC1g+rld6i6ptbGKa9TYLB/29V9g2WcqbF1gRKiLaZ4T8XcFYSDUpHbDAQTlV3BipF2qiUwIDAQABAkBxEGzEC36d0H5gkQFHVHZfc0ydmgk+mxdoWa7EG5vVzeuXOnfDjBFbNc6/qTRt/DLUZQivpPCc1b8yqCPt6vsBAiEA97jUzaMLR76sceoeWXC+pOOpLUKHHaGwcIWff0WN4XsCIQDquEPnVWJX3Sf8WM2Oh690HCrUZWnqbMMOtDPuw06PCQIhAIAx01ZziLPhFaaBzKDmepIkCnmUkw2fjgo87DRVzz2fAiEArOSCEBqS0Am1dGmtMDm+AZbl7YGuHOF0ZJZUUqFGcpECIHY1NU7bjZAYiAna5VkWzkN5unYAvvSXe+sXEEP07Ogv");
        
        Result result = SDK.init(pc, null, null);
        if (0 != result.getErrCode()) {
            LOG.error("初始化失败，错误码=" + result.getErrCode() + ",错误信息="
                    + result.getMsg());
            return;
        }
        LOG.error("errCode:" + result.getErrCode() + ",msg:" + result.getMsg());
        
//        //登录
//		LOG.info("初始化成功，开始执行项目登录...");
//        // 使用初始化设置的项目编号和项目验证码做项目账户登录
//        LoginResult loginResult = SDK.login();
//
//        LOG.info("验证处理成功! accountId:" + loginResult.getAccountId());
        
        //创建企业账户&法人账户
//        try {
//			financeSignatureActionApi.addOrganize();
//		} catch (Exception e) {
//			LOG.error(e.getMessage());
//		}
	}


}
